package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.innovation.chengzhangriji.R;

public class SelectLayout extends LinearLayout implements View.OnClickListener {

    public interface OnClickSelectListener {
        void onClickSelect(View view);
    }

    private OnClickSelectListener mOnClickSelectListener;

    private CuteImageView mCiv;

    private CuteTextView mCtv;

    public SelectLayout(Context context) {
        super(context);
        init(context, null);
    }

    public SelectLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SelectLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @Override
    public void onClick(View v) {
        if (mOnClickSelectListener != null) {
            mOnClickSelectListener.onClickSelect(this);
        }
    }

    public void setOnClickSelectListener(OnClickSelectListener onClickSelectListener) {
        mOnClickSelectListener = onClickSelectListener;
    }

    public Drawable getSelectSrc() {
        return mCiv.getBackground();
    }

    public void setSelectSrc(Drawable drawable) {
        mCiv.setBackgroundDrawable(drawable);
    }

    public String getSelectText() {
        return mCtv.getText().toString();
    }

    public void setSelectText(String text) {
        mCtv.setText(text);
    }

    private void init(Context context, AttributeSet attrs) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SelectLayout);
        Drawable selectSrc = typedArray.getDrawable(R.styleable.SelectLayout_select_src);
        String selectText = typedArray.getString(R.styleable.SelectLayout_select_text);
        typedArray.recycle();

        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_activity_select, this);

        mCiv = (CuteImageView) view.findViewById(R.id.btn_select);
        if (selectSrc != null) {
            mCiv.setCuteImageDrawable(selectSrc);
        }
        mCiv.setOnClickListener(this);

        mCtv = (CuteTextView) view.findViewById(R.id.text_select);
        if (!TextUtils.isEmpty(selectText)) {
            mCtv.setText(selectText);
        }
    }

}
